<!doctype html>
<html>
<head>
    <title>{% block title %}{{ title }}{% endblock %}</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    {% block styles %}{% endblock %}
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --success-color: #4cc9f0;
            --info-color: #4895ef;
            --warning-color: #f72585;
            --dark-color: #212529;
            --light-color: #f8f9fa;
            /* 修改侧边栏宽度为更窄的尺寸 */
            --sidebar-width: 180px;
            --topbar-height: 60px;
        }
        
        body {
            background-color: #f5f7fb;
            font-family: 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            overflow-x: hidden;
        }
        
        .sidebar {
            height: 100vh;
            position: fixed;
            top: 0;
            left: 0;
            width: var(--sidebar-width);
            /* 使用浅亮色主题 */
            background: linear-gradient(180deg, #ffffff, #f0f4f8);
            color: #333;
            padding-top: var(--topbar-height);
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
            z-index: 1000;
            transition: all 0.3s;
            border-right: 1px solid #e6e9ed;
        }
        
        .sidebar .nav-link {
            padding: 12px 20px;
            /* 使用浅色文字，激活时使用深色 */
            color: #5a677d;
            border-radius: 0;
            margin: 5px 10px;
            transition: all 0.3s;
            font-weight: 500;
            white-space: nowrap; /* 添加这一行防止文本换行 */
            overflow: hidden; /* 添加这一行隐藏溢出内容 */
            text-overflow: ellipsis; /* 添加这一行用省略号表示溢出文本 */
        }
        
        .sidebar .nav-link:hover,
        .sidebar .nav-link:focus {
            color: #2d3748;
            background-color: rgba(67, 97, 238, 0.1);
        }
        
        .sidebar .nav-link.active {
            background-color: rgba(67, 97, 238, 0.2);
            color: #4361ee;
            border-radius: 8px;
            font-weight: 500;
        }
        
        .sidebar .nav-link i {
            margin-right: 10px;
            width: 20px;
            text-align: center;
        }
        
        .main-content {
            margin-left: var(--sidebar-width);
            padding-top: calc(var(--topbar-height) + 20px); /* 增加顶部间距 */
            padding-left: 15px;
            padding-right: 15px;
            min-height: calc(100vh - var(--topbar-height));
        }
        
        .topbar {
            background-color: white;
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
            height: var(--topbar-height);
            position: fixed;
            top: 0;
            left: var(--sidebar-width);
            right: 0;
            z-index: 999;
        }
        
        .topbar .navbar-text {
            color: var(--dark-color);
            font-weight: 500;
        }
        
        .topbar .nav-link {
            color: var(--dark-color);
            font-weight: 500;
            padding: 10px 15px;
            border-radius: 8px;
            transition: all 0.3s;
        }
        
        .topbar .nav-link:hover {
            background-color: var(--light-color);
        }
        
        .card {
            border: none;
            border-radius: 12px;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.05);
            margin-bottom: 24px;
            transition: all 0.3s;
        }
        
        .card:hover {
            box-shadow: 0 0.75rem 1.5rem rgba(0, 0, 0, 0.09);
        }
        
        .card-header {
            background-color: white;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            font-weight: 600;
            border-radius: 12px 12px 0 0 !important;
            padding: 16px 24px;
        }
        
        .card-body {
            padding: 24px;
        }
        
        .btn {
            border-radius: 8px;
            padding: 8px 16px;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        .btn-primary {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
        }
        
        .btn-primary:hover {
            background-color: var(--secondary-color);
            border-color: var(--secondary-color);
            transform: translateY(-2px);
        }
        
        .btn-success {
            background-color: var(--success-color);
            border-color: var(--success-color);
        }
        
        .table {
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.05);
            border-radius: 12px;
            overflow: hidden;
        }
        
        .table thead th {
            background-color: var(--primary-color);
            color: white;
            border: none;
            font-weight: 500;
        }
        
        .table-hover tbody tr:hover {
            background-color: rgba(67, 97, 238, 0.05);
        }
        
        .alert {
            border-radius: 8px;
            font-size: 0.9rem;
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.05);
        }
        
        /* 添加固定定位的alert样式，使其不影响页面布局 */
        .alert-fixed {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1050;
            width: 300px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
        }
        
        h1, h2, h3, h4, h5, h6 {
            color: var(--dark-color);
            font-weight: 600;
        }
        
        .content-header {
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
            margin-bottom: 24px;
        }
        
        .content-header h1 {
            margin: 0;
            font-size: 1.75rem;
        }
        
        /* 终端样式 */
        .terminal-output {
            white-space: pre-wrap;
            font-size: 14px;
            line-height: 1.4;
        }
        
        /* 响应式设计 */
        @media (max-width: 992px) {
            .sidebar {
                width: 70px;
                text-align: center;
            }
            
            .sidebar .nav-link span {
                display: none;
            }
            
            .sidebar .nav-link i {
                margin-right: 0;
                font-size: 1.2rem;
            }
            
            .main-content {
                margin-left: 70px;
                padding-top: calc(var(--topbar-height) + 20px); /* 保持一致的间距 */
            }
            
            .topbar {
                left: 70px;
            }
        }
        
        @media (max-width: 768px) {
            .sidebar {
                width: 0;
                overflow: hidden;
            }
            
            .main-content {
                margin-left: 0;
                padding-top: calc(var(--topbar-height) + 20px); /* 保持一致的间距 */
            }
            
            .topbar {
                left: 0;
            }
        }

        .model-status-group {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            background: #f8f9fa;
            border-radius: 8px;
            padding: 4px 10px;
            margin-right: 1rem;
        }
        #check-model-status {
            min-width: 90px;
        }
        #model-status {
            font-size: 0.97rem;
            margin-bottom: 0;
        }
        #model-status-text {
            font-weight: 600;
            display: inline-block;
            width: 100px; /* 设置固定宽度 */
            text-align: center; /* 居中对齐 */
            white-space: nowrap; /* 防止换行 */
        }
        #current-model-name {
            display: inline-block;
            width: 180px; /* 设置更长的宽度 */
            text-align: center; /* 居中对齐 */
            white-space: nowrap; /* 防止换行 */
        }
        @media (max-width: 768px) {
            .model-status-group {
                margin-right: 0;
                padding: 2px 6px;
                font-size: 0.92rem;
            }
            #check-model-status {
                min-width: 70px;
                padding: 4px 8px;
                font-size: 0.92rem;
            }
            #model-status-text {
                width: 80px; /* 响应式调整宽度 */
            }
            #current-model-name {
                width: 140px; /* 响应式调整为更长宽度 */
            }
        }
    </style>
</head>
<body>
    <!-- 顶部导航栏 -->
    <nav class="navbar navbar-expand navbar-light topbar">
        <div class="container-fluid">
            <a class="navbar-brand fw-bold text-primary" href="{{ url_for('admin.index') }}">
                管理系统
            </a>
            <ul class="navbar-nav ms-auto">
                {% if current_user.is_authenticated %}
                    {# 仅仪表盘显示模型服务和当前模型状态栏 #}
                    {% if request.endpoint == 'admin.dashboard' %}
                        <li class="nav-item d-flex align-items-center model-status-group">
                            <span id="model-status" class="navbar-text">
                                <i class="bi bi-cpu me-1"></i>
                                <span style="font-weight:500;">模型服务:</span>
                                <span id="model-status-text" class="text-muted">未检测</span>
                            </span>
                            <i class="bi bi-arrow-clockwise text-info" id="refresh-model-status" style="cursor: pointer;"></i>
                        </li>
                        <li class="nav-item d-flex align-items-center model-status-group">
                            <span id="current-model" class="navbar-text">
                                <i class="bi bi-box"></i>
                                <span style="font-weight:500;">当前模型:</span>
                                <span id="current-model-name" class="text-muted">加载中...</span>
                            </span>
                            <i class="bi bi-arrow-clockwise text-info" id="refresh-model-name" style="cursor: pointer;"></i>
                        </li>
                    {% else %}
                        {# 其他页面暂时注释掉模型服务和当前模型栏 #}
                        {#
                        <li class="nav-item d-flex align-items-center model-status-group">
                            <span id="model-status" class="navbar-text">
                                <i class="bi bi-cpu me-1"></i>
                                <span style="font-weight:500;">模型服务:</span>
                                <span id="model-status-text" class="text-muted">未检测</span>
                            </span>
                            <i class="bi bi-arrow-clockwise text-info" id="refresh-model-status" style="cursor: pointer;"></i>
                        </li>
                        <li class="nav-item d-flex align-items-center model-status-group">
                            <span id="current-model" class="navbar-text">
                                <i class="bi bi-box"></i>
                                <span style="font-weight:500;">当前模型:</span>
                                <span id="current-model-name" class="text-muted">加载中...</span>
                            </span>
                            <i class="bi bi-arrow-clockwise text-info" id="refresh-model-name" style="cursor: pointer;"></i>
                        </li>
                        #}
                    {% endif %}
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('auth.logout') }}">
                            <i class="bi bi-box-arrow-right"></i> 退出
                        </a>
                    </li>
                {% else %}
                    <a class="nav-link" href="{{ url_for('auth.login') }}">登录</a>
                {% endif %}
            </ul>
        </div>
    </nav>
    
    <div class="container-fluid">
        <div class="row">
            <!-- 左侧菜单栏 -->
            <nav class="col-md-3 col-lg-2 d-md-block sidebar">
                <div class="position-sticky pt-3">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'admin.dashboard' %}active{% endif %}" 
                               href="{{ url_for('admin.dashboard') }}">
                                <i class="bi bi-speedometer2"></i> <span>仪表盘</span>
                            </a>
                        </li>
                        <!-- 只保留用户管理和模型切换两个功能 -->
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'admin.users' %}active{% endif %}" 
                               href="{{ url_for('admin.users') }}">
                                <i class="bi bi-people"></i> <span>用户管理</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'admin.model_switch' %}active{% endif %}" 
                               href="{{ url_for('admin.model_switch') }}">
                                <i class="bi bi-arrow-repeat"></i> <span>模型切换</span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'admin.model_chat' %}active{% endif %}" 
                               href="{{ url_for('admin.model_chat') }}">
                                <i class="bi bi-chat-dots"></i> <span>模型测试</span>
                            </a>
                        </li>
                        <!-- 添加性能测试菜单项 -->
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'admin.performance' %}active{% endif %}" 
                               href="{{ url_for('admin.performance') }}">
                                <i class="bi bi-bar-chart-line"></i> <span>性能测试</span>
                            </a>
                        </li>
                        <!-- 添加终端菜单项 -->
                        <!--
                        <li class="nav-item">
                            <a class="nav-link {% if request.endpoint == 'admin.terminal' %}active{% endif %}" 
                               href="{{ url_for('admin.terminal') }}">
                                <i class="bi bi-terminal"></i> <span>终端</span>
                            </a>
                        </li>
                        -->
                    </ul>
                </div>
            </nav>
            
            <!-- 主内容区域 -->
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
                {% with messages = get_flashed_messages() %}
                    {% if messages %}
                        {% for message in messages %}
                            <div class="alert alert-info alert-dismissible fade show mt-3 alert-fixed" role="alert">
                                {{ message }}
                                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                            </div>
                        {% endfor %}
                    {% endif %}
                {% endwith %}
                
                {% block content %}{% endblock %}
            </main>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入 Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    {% block scripts %}
<script>
    // 仅仪表盘页面启用模型服务和当前模型检测 JS
    {% if request.endpoint == 'admin.dashboard' %}
    document.addEventListener('DOMContentLoaded', function() {
        const statusText = document.getElementById('model-status-text');
        const refreshStatusIcon = document.getElementById('refresh-model-status');
        const modelNameSpan = document.getElementById('current-model-name');
        const refreshModelIcon = document.getElementById('refresh-model-name');

        function checkModelStatus() {
            statusText.textContent = '加载中...';
            statusText.className = 'text-warning';

            fetch('/model_status')
                .then(response => {
                    if (response.ok) {
                        return response.text();
                    } else {
                        throw new Error('服务未响应');
                    }
                })
                .then(data => {
                    statusText.textContent = '模型服务已启动';
                    statusText.className = 'text-success';
                })
                .catch(error => {
                    statusText.textContent = '模型服务未启动';
                    statusText.className = 'text-danger';
                });
        }

        function fetchModelName() {
            modelNameSpan.textContent = '加载中...';
            modelNameSpan.className = 'text-warning';

            fetch('/current_model')
                .then(response => response.json())
                .then(data => {
                    if (data.model_name) {
                        modelNameSpan.textContent = data.model_name;
                        modelNameSpan.className = 'text-success';
                    } else {
                        throw new Error(data.error || '未知错误');
                    }
                })
                .catch(error => {
                    modelNameSpan.textContent = '无';
                    modelNameSpan.className = 'text-danger';
                    console.error('模型名称获取失败:', error);
                });
        }

        if (refreshStatusIcon) {
            refreshStatusIcon.addEventListener('click', checkModelStatus);
        }

        if (refreshModelIcon) {
            refreshModelIcon.addEventListener('click', fetchModelName);
        }

        // 只有首次访问自动刷新
        if (!localStorage.getItem('modelStatusChecked')) {
            checkModelStatus();
            fetchModelName();
            localStorage.setItem('modelStatusChecked', '1');
        }
    });
    {% else %}
    // 其他页面暂时注释掉模型服务和当前模型检测 JS
    /*
    document.addEventListener('DOMContentLoaded', function() {
        // ...模型服务和当前模型检测相关JS...
    });
    */
    {% endif %}
</script>
{% endblock %}
</body>
</html>